home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / dviselect / conv.h < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-16  |  3.2 KB  |  83 lines

  1. /*
  2.  * Copyright (c) 1987 University of Maryland Department of Computer Science.
  3.  * All rights reserved.  Permission to copy for any purpose is hereby granted
  4.  * so long as this copyright notice remains intact.
  5.  */
  6.  
  7. /*
  8.  * Conversions.  Conversion factors convert between values in scaled
  9.  * points and values in device-dependenent units.  The results of all
  10.  * conversions are rounded to the nearest integral value, of type (i32).
  11.  */
  12.  
  13. /*
  14.  * This is now done using `double' values, but may be changed to
  15.  * fixed-point or some other `fast' method, as long as the results
  16.  * are consistent and reasonably accurate.  The structure `conversion'
  17.  * holds the conversion-method-dependent quantities; the macros
  18.  * fromSP and toSP apply the conversion to a value.  (Note that
  19.  * fromSP and toSP need not be macros, but should be fast.)
  20.  *
  21.  * SetConversion sets the (single, global) conversion factor.
  22.  * If a driver needs special conversions, there is another routine,
  23.  * CSetConversion that sets a specific conversion, and cfromSP and
  24.  * ctoSP to apply these.
  25.  *
  26.  * IS USING DOTS PER INCH SUFFICIENT?  (Pixels per point might be better.)
  27.  *
  28.  * Note that it is necessary to set the global conversion factor before
  29.  * using any fonts.
  30.  */
  31. struct conversion {
  32.     double    c_fromsp;    /* multiplier to convert from scaled points */
  33.     double    c_tosp;        /* multiplier to convert to scaled points:
  34.                    could divide by c_fromsp, but this should
  35.                    be faster and more accurate */
  36.     double    c_mag;        /* the magnification this conversion
  37.                    represents; mainly for GetFont() */
  38.     double    c_dpi;        /* dpi (should be pixels per point?) */
  39. } Conversion;
  40.  
  41. /*
  42.  * In order to do this, we need to round properly.  The compilers I
  43.  * have tend to generate very poor code for this.  The following is
  44.  * intended to help them out.  Smarter compilers can do better, but
  45.  * if they are smart enough, they will realise that the variables
  46.  * here are not used anywhere else, and discard them.  (For a compiler
  47.  * to do this given separate compliation, `static' is a must.)
  48.  */
  49. #ifdef lint            /* or a smart compiler */
  50. #define    ROUND(f) ((i32) ((f) < 0.0 ? (f) - 0.5 : (f) + 0.5))
  51. #define    CEIL(f)    ((double) (i32) (f) < (f) ? (i32) (f) + 1 : (i32) (f))
  52. #else
  53. static double _half = 0.5;
  54. static double _zero = 0.0;
  55. static double _d;
  56. #define    ROUND(f) ((i32) (_d = (f), _d < _zero ? _d - _half : _d + _half))
  57. #ifdef NEGATIVE_FLOAT_ROUNDS_TO_NEGATIVE_INFINITY
  58. #define    CEIL(f)  (-(i32) -(f))
  59. #else /* we will assume that floating to integer truncates */
  60. static i32 _i;
  61. #define    CEIL(f)     (_i = _d = (f), _i < _d ? _i + 1 : _i)
  62. #endif /* round towards negative infinity */
  63. #endif /* lint */
  64.  
  65. #define    SetConversion(dpi, usermag, num, denom, dvimag)    \
  66.     CSetConversion(&Conversion, dpi, usermag, num, denom, dvimag)
  67.  
  68. #define    cfromSP(c, v)    ROUND((c)->c_fromsp * (v))
  69. #define    ctoSP(c, v)    ROUND((c)->c_tosp * (v))
  70.  
  71. #define    fromSP(v)    cfromSP(&Conversion, v)
  72. #define    toSP(v)        ctoSP(&Conversion, v)
  73.  
  74. /*
  75.  * Conversions for rules are a bit different: we must round up, rather
  76.  * than off.  ConvRule applies the global conversion value for a rule
  77.  * value (height or width); CConvRule applies a specific conversion.
  78.  */
  79. #define    CConvRule(c, v)    CEIL((c)->c_fromsp * (v))
  80. #define    ConvRule(v)    CConvRule(&Conversion, v)
  81.  
  82. void    CSetConversion();
  83.